package com.briup.jz.service.impl;

import com.briup.jz.bean.OrderComment;
import com.briup.jz.bean.OrderCommentExample;
import com.briup.jz.dao.OrderCommentMapper;
import com.briup.jz.service.IOrderCommentService;
import com.briup.jz.utils.CustomerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderCommentImpl implements IOrderCommentService {
	@Autowired
	private OrderCommentMapper orderCommentMapper;

	@Override
	public void saveOrUpdate(OrderComment orderComment) throws CustomerException {
//        判断订单id是否存在
//        如果id 不为空则插入信息

		if (orderComment.getId() == null) {
			orderCommentMapper.insert(orderComment);
		} else {

//        修改评论
			orderCommentMapper.updateByPrimaryKey(orderComment);

		}
	}

	@Override
	public void deleteByid(Long id) throws CustomerException {
		OrderComment orderComment = orderCommentMapper.selectByPrimaryKey(id);
//根据主键查找评论信息
		if (orderComment == null) {// 如果评论为空
			throw new CustomerException("你删除的评论不存在");
		}
		orderCommentMapper.deleteByPrimaryKey(id);
	}

	@Override
	public List<OrderComment> query(Long id) {
		OrderCommentExample example = new OrderCommentExample();
		// 判断用户id是否为空
		if (id != null) {
//            如果不为空则根据顾客id 查询,否则则查询所有
			example.createCriteria().andCustomerIdEqualTo(id);
		}
		return orderCommentMapper.selectByExample(example);
	}

	@Override
	public void checkNoPass(Long id) throws  CustomerException{
		// TODO Auto-generated method stub
		OrderComment orderComment=orderCommentMapper.selectByPrimaryKey(id);
		if(orderComment==null) {
			throw new CustomerException();
		}
//		订单评论存在
		orderComment.setStatus("不通过");
		saveOrUpdate(orderComment);
		
	}

	@Override
	public void checkPass(Long id) {
		// TODO Auto-generated method stub
		OrderComment orderComment=orderCommentMapper.selectByPrimaryKey(id);
		if(orderComment==null) {
			throw new CustomerException();
		}
//		订单评论存在
		orderComment.setStatus("通过");
		saveOrUpdate(orderComment);
	}
}
